﻿using Asp.Versioning;
using FlyingEye.ControllerServices;
using FlyingEye.Exceptions;
using FlyingEye.Monitors.Models;
using FlyingEye.Monitors.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Authorize]
    [ControllerName("StringValueMonitorClient")]
    [ApiVersion("1.0")]
    [Route("api/device-monitor-service/v{version:apiVersion}/string-value-monitor-clients")]
    public class StringValueMonitorClientController : DefaultController
    {
        private readonly IStringValueMonitorClientRepository _stringValueMonitorClientRepository;

        public StringValueMonitorClientController(IStringValueMonitorClientRepository stringValueMonitorClientRepository)
        {
            _stringValueMonitorClientRepository = stringValueMonitorClientRepository;
        }

        [HttpGet("{id}", Name = "GetByStringValueMonitorClientId")]
        public async Task<ActionResult<StringValueMonitorClientResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var model = await _stringValueMonitorClientRepository.GetAsync(id);

                return ObjectMapper.Map<StringValueMonitorClientModel, StringValueMonitorClientResponse>(model);
            });
        }

        [HttpGet]
        public async Task<ActionResult<List<StringValueMonitorClientResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                var list = await _stringValueMonitorClientRepository.GetListAsync();
                return ObjectMapper.Map<List<StringValueMonitorClientModel>, List<StringValueMonitorClientResponse>>(list);
            });
        }

        [HttpPut]
        public async Task<ActionResult<StringValueMonitorClientResponse>> UpdateAsync([FromBody] UpdateStringValueMonitorClientRequest request)
        {
            if (request.Id == Guid.Empty)
            {
                throw new AppArgumentValidationException("ID 不能为空");
            }

            if (request.ReadStringValueTaskId == Guid.Empty)
            {
                throw new AppArgumentValidationException("监听任务 ID 不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.ClientName))
            {
                throw new AppArgumentValidationException("监听点客户端名称不能为空");
            }

            return await this.PutInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<UpdateStringValueMonitorClientRequest, StringValueMonitorClientModel>(request);
                var result = await _stringValueMonitorClientRepository.UpdateAsync(model);
                return ObjectMapper.Map<StringValueMonitorClientModel, StringValueMonitorClientResponse>(result);
            });
        }

        [HttpPost]
        public async Task<ActionResult<StringValueMonitorClientResponse>> CreateAsync([FromBody] CreateStringValueMonitorClientRequest request)
        {
            if (request.ReadStringValueTaskId == Guid.Empty)
            {
                throw new AppArgumentValidationException("监听任务 ID 不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.ClientName))
            {
                throw new AppArgumentValidationException("监听点客户端名称不能为空");
            }

            return await this.InsertInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<CreateStringValueMonitorClientRequest, StringValueMonitorClientModel>(request);
                var result = await _stringValueMonitorClientRepository.InsertAsync(model);
                return ObjectMapper.Map<StringValueMonitorClientModel, StringValueMonitorClientResponse>(result);
            }, "GetByStringValueMonitorClientId", x => new { id = x.Id });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _stringValueMonitorClientRepository.DeleteAsync(id);
            });
        }
    }
}
